package com.lzj.servlet;

import com.lzj.dao.UserDao;
import com.lzj.dao.impl.UserDaoImpl;
import com.lzj.entity.*;
import com.lzj.service.impl.UserServiceImpl;
import com.mysql.cj.Session;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @autor:lzj
 * @date:2021/12/2
 */
public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("pwd");
        System.out.println(username);
        System.out.println(password);
        UserServiceImpl userService = new UserServiceImpl();
        UserDao userDao = new UserDaoImpl();
        userService.setUserDao(userDao);
        User user = userService.queryUser(username,password);
        if(user!=null){
            req.getSession().setAttribute("username",username);
            req.getSession().setAttribute("userId",user.getId());
            //个人信息查询显示
            UserInfomation userInfomation = userService.queryUserInf(user.getId());
            req.getSession().setAttribute("userInfomation",userInfomation);
            //地址查询显示
            Address address = userDao.queryAddress(user.getId());
            req.getSession().setAttribute("address",address);
            //购物车
            List<ShoppCart> shoppCarts = userService.queryShoppCart(user.getId());
            req.getSession().setAttribute("shoppCarts",shoppCarts);
            //订单
            List<Order> orders = userService.queryOrder(user.getId());
            req.getSession().setAttribute("orders",orders);
            //订单数据
            List<OrderData> orderData = userService.queryOrderData(user.getId());
            req.getSession().setAttribute("orderData",orderData);

            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }else{
//            resp.getWriter().print("用户名或密码错误，请重新输入！");
            req.setAttribute("msg","用户名或密码错误，请重新输入！");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
